package cn.ximart.passport.controller;


import cn.ximart.passport.pojo.vo.UserListItemVO;
import cn.ximart.passport.service.IUserService;
import cn.ximart.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api("用户管理")
@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;


    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 400)
    @PostMapping("/login")
    public JsonResult reg(@RequestBody UserDTO userDTO) {
        log.debug("即将进行登录操作，接受的数据为：{}", userDTO);
        String jwt = userService.login(userDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("用户总数")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("/count-users")
    public JsonResult countAllUser(){
        log.debug("接收到查询用户数量的请求");
        int count = userService.countUsers();
        return JsonResult.ok(count);
    }


    @ApiOperation("用户列表")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询用户列表的请求");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }


}
